package com.ruoyi.project.cyq.item.item.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cyq.client.client.domain.Client;
import com.ruoyi.project.cyq.client.client.service.IClientService;
import com.ruoyi.project.cyq.item.archiving.domain.ConvertTime;
import com.ruoyi.project.cyq.item.archiving.service.IArchivingService;
import com.ruoyi.project.cyq.item.item.domain.Item;
import com.ruoyi.project.cyq.item.item.domain.ItemVo;
import com.ruoyi.project.cyq.item.item.service.IItemService;
import com.ruoyi.project.cyq.order.domain.Order;
import com.ruoyi.project.cyq.order.domain.OrderProduct;
import com.ruoyi.project.cyq.order.service.IOrderService;
import com.ruoyi.project.cyq.personnelManagement.personnel.domain.Personnel;
import com.ruoyi.project.cyq.personnelManagement.personnel.service.IPersonnelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author Strawberry
 * @date 2020-09-17 下午 2:11
 * @desc 快乐将永远依着草莓
 * @desc 项目管理信息查看模块的控制器
 */

@Controller
@RequestMapping("/cyq/item/item")
public class ItemController extends BaseController {

    private String prefix = "cyq/item/info";

    /**
     * 项目的service
     */
    @Autowired
    private IItemService itemService;
    /**
     * 员工的service
     */
    @Autowired
    private IPersonnelService personnelService;
    /**
     * 客户的service
     */
    @Autowired
    private IClientService iClientService;
    /**
     * 订单 service
     */
    @Autowired
    private IOrderService iOrderService;
    /**
     * 产品 service
     */
    @Autowired
    private IArchivingService archivingService;


    /**
     * 管理主页的控制器
     */
    @RequiresPermissions("cyq:item:view")
    @GetMapping()
    public String item() {
        return prefix + "/item";
    }

    /**
     * 项目的展示
     */
    @RequiresPermissions("cyq:item:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo showAllItem(ItemVo item) {
//        startPage();
        List<Item> selectAllshow = itemService.selectItemList(item);
        return getDataTable(selectAllshow);
    }

    /**
     * 项目查看具体信息
     * 【项目，员工，客户，订单，产品】
     */
    @RequiresPermissions("cyq:item:detail")
    @GetMapping("/detail/{projectId}")
    public String showItemDetail(@PathVariable BigInteger projectId, ModelMap modelMap) {
//        通过项目的id查项目
        ItemVo itemVo = itemService.selectOneById(projectId);
//        通过项目获取 员工的Id查询员工姓名
        Personnel personnel = personnelService.selectPersonnelById(itemVo.getLeaderId());
        itemVo.setLeaderName(personnel.getStaffName());

//        通过项目获取 客户的id查询客户的信息
        Client client = iClientService.selectOneById(itemVo.getClientId());

//        项目==>订单Id==>产品id
        Order order = iOrderService.selectOrderByOrderNumber(Long.valueOf(itemVo.getOrderNumber()));
        OrderProduct orderProduct = null;
        if (order != null) {
            orderProduct = archivingService.selectProjectById(order.getProductId());
        } else {
            orderProduct = new OrderProduct();
            orderProduct.setProductId(-1);
        }

        modelMap.addAttribute("oneItem", itemVo);
        modelMap.addAttribute("client", client);
        modelMap.addAttribute("orderProduct", orderProduct);

        return prefix + "/detail";
    }


    /**
     * 项目的修改跳转
     */
    @RequiresPermissions("cyq:item:edit")
    @GetMapping("/edit/{projectId}")
    public String addItem(@PathVariable BigInteger projectId, Model model,Personnel p) {
//        获取项目数据，进行回显
        ItemVo itemVo = itemService.selectOneById(projectId);

//        获取所有职员信息进行回写
        List<Personnel> staffList = personnelService.selectPersonnelList(p);
//        处理时间
        String startTime = timeToString(itemVo.getProjectStartTime());
        String endTime = timeToString(itemVo.getProjectEndTime());
        ConvertTime convertTime = new ConvertTime(startTime,endTime);
        System.out.println(convertTime);

//        通过项目获取 客户的id查询客户的信息
        Client client = iClientService.selectOneById(itemVo.getClientId());
//        项目==>订单Id==>产品id
        Order order = iOrderService.selectOrderByOrderNumber(Long.valueOf(itemVo.getOrderNumber()));
        OrderProduct orderProduct = null;
        if (order != null) {
            orderProduct = archivingService.selectProjectById(order.getProductId());
        } else {
            orderProduct = new OrderProduct();
            orderProduct.setProductId(-1);
        }

        model.addAttribute("oneItem", itemVo);
        model.addAttribute("convertTime", convertTime);
        model.addAttribute("client", client);
        model.addAttribute("orderProduct", orderProduct);
        model.addAttribute("staffList", staffList);

        return prefix + "/edit";
    }

    /**
     * 一个时间转换方法
     * */
    public static String timeToString(Date projectTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String timeFormat = sdf.format(projectTime);
        return timeFormat;
    }

    /**
     * 修改数据的save方法
     * */
    @PostMapping("/edit")
    @ResponseBody
     public AjaxResult saveUpdateItem(Item itemVo,ConvertTime convertTime){
         return toAjax(itemService.updateTheItem(itemVo,convertTime));
     }

    /**
     * 删除功能【如果订单不存在，则假删除项目】
     * */
    @RequiresPermissions("cyq:item:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(BigInteger [] ids){
        return toAjax(itemService.deleteByIds(ids));
    }

}
